package cn.itcast.service.impl;

import cn.itcast.domain.Company;
import cn.itcast.factory.MapperFactory;
import cn.itcast.mapper.CompanyMappe;
import cn.itcast.service.CompanyService;
import cn.itcast.utils.TransactionUtil;
import cn.itcast.utils.UuidUtils;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;


/**
 * company业务处理层    增删改功能记得提交事务,所有功能都要释放资源
 */
public class CompanyServiceImpl implements CompanyService {

    //查询所有
    public List<Company> findAll(int pageNum) {
        //获取session
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取mapper对象
        CompanyMappe mapper = sqlSession.getMapper(CompanyMappe.class);
        PageHelper.startPage(pageNum, 10);
        List<Company> companies = mapper.findAll();
        //释放资源
        TransactionUtil.close(sqlSession);
        return companies;
    }

    @Override
    public void add(Company company) {
        //获取session
        SqlSession sqlSession = MapperFactory.getSqlSession();
        try {
            //获取mapper对象
            CompanyMappe mapper = sqlSession.getMapper(CompanyMappe.class);
            String uuid = UuidUtils.getUuid();

            company.setId(uuid);
            mapper.add(company);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }


    }

    //查询功能,根据id
    public Company findById(String id) {
        //获取session
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取mapper对象
        CompanyMappe mapper = sqlSession.getMapper(CompanyMappe.class);

        Company company = mapper.findById(id);
        //释放资源
        TransactionUtil.close(sqlSession);
        return company;
    }

    @Override
    public void update(Company company) {
        //获取session
        SqlSession sqlSession = MapperFactory.getSqlSession();
        try {
            //获取mapper对象
            CompanyMappe mapper = sqlSession.getMapper(CompanyMappe.class);
            mapper.update(company);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }


    }

    //批量删除功能
    public void deleteByIds(String ids) {
        if (ids != null && ids.length() > 0) {

            String[] id = ids.split(",");

            //获取session
            SqlSession sqlSession = MapperFactory.getSqlSession();
            try {
                //获取mapper对象
                CompanyMappe mapper = sqlSession.getMapper(CompanyMappe.class);
                mapper.deleteByIds(id);
                TransactionUtil.commit(sqlSession);
            } catch (Exception e) {
                TransactionUtil.rollback(sqlSession);
                e.printStackTrace();
            } finally {
                TransactionUtil.close(sqlSession);
            }

        }

    }

    @Override
    public List<Company> findLikeName(String val, int pageNum) {
        //获取session
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取mapper对象
        CompanyMappe mapper = sqlSession.getMapper(CompanyMappe.class);
        PageHelper.startPage(1, 2);
        List<Company> companies = mapper.findLikeName(val);
        //释放资源
        TransactionUtil.close(sqlSession);
        return companies;

    }
}
